package com.yunpeng.aidevice.biz.service.impl;

import com.yunpeng.aidevice.biz.service.IMerchantNoticeMailBizService;
import com.yunpeng.aidevice.common.dto.save.MailSenderSaveDTO;
import com.yunpeng.aidevice.common.enums.ResultCodeEnum;
import com.yunpeng.aidevice.common.vo.NoticeMailConfigVO;
import com.yunpeng.aidevice.dao.model.record.MerchantNoticeMailDO;
import com.yunpeng.aidevice.dao.service.IMerchantNoticeMailService;
import com.yunpeng.common.dto.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * MerchantNoticeMailBizServiceImpl
 *
 * @author : Moore(hemw)
 * @date : 2020-05-04
 */
@Service
public class MerchantNoticeMailBizServiceImpl implements IMerchantNoticeMailBizService {

    @Resource
    private IMerchantNoticeMailService merchantNoticeMailService;

    @Override
    /**
     * 获取默认邮箱配置
     * @param tenantCode
     * @return
     */
    public Result<NoticeMailConfigVO> getDefaultMailConfig(String tenantCode) {
        MerchantNoticeMailDO merchantNoticeMailDO = merchantNoticeMailService.getDefaultNoticeMailConfig(tenantCode);
        if (merchantNoticeMailDO == null) {
            return Result.success();
        }
        NoticeMailConfigVO noticeMailConfigVO = new NoticeMailConfigVO(merchantNoticeMailDO.getMerchantCode(), merchantNoticeMailDO.getProtocol(), merchantNoticeMailDO.getMailHost(), merchantNoticeMailDO.getSenderEmail(), merchantNoticeMailDO.getSenderName(), merchantNoticeMailDO.getPassword(), merchantNoticeMailDO.getPort());
        return Result.success(noticeMailConfigVO);
    }

    @Override
    /**
     * 获取商户邮箱配置
     * @param merchantCode
     * @param tenantCode
     * @return
     */
    public Result<NoticeMailConfigVO> getMerchantMailConfig(String merchantCode, String tenantCode) {
        MerchantNoticeMailDO merchantNoticeMailDO = merchantNoticeMailService.getMerchantNoticeMailConfig(merchantCode, tenantCode);
        if (merchantNoticeMailDO == null) {
            return Result.success();
        }
        NoticeMailConfigVO noticeMailConfigVO = new NoticeMailConfigVO(merchantNoticeMailDO.getMerchantCode(), merchantNoticeMailDO.getProtocol(), merchantNoticeMailDO.getMailHost(), merchantNoticeMailDO.getSenderEmail(), merchantNoticeMailDO.getSenderName(), merchantNoticeMailDO.getPassword(), merchantNoticeMailDO.getPort());
        return Result.success(noticeMailConfigVO);
    }

    @Override
    /**
     * 新增邮箱配置
     *
     * @param mailSenderSaveDTO
     * @param operatorCode
     * @return
     */
    public Result<Void> addNewConfig(MailSenderSaveDTO mailSenderSaveDTO, String operatorCode) {
        if (merchantNoticeMailService.addNewMailSender(mailSenderSaveDTO, operatorCode)) {
            return Result.success();
        }
        return Result.fail(ResultCodeEnum.SAVE_DATA_FAILED);
    }

    @Override
    /**
     * 更新邮箱配置
     *
     * @param mailSenderSaveDTO
     * @param operatorCode
     * @return
     */
    public Result<Void> updateMailSender(MailSenderSaveDTO mailSenderSaveDTO, String operatorCode) {
        if (merchantNoticeMailService.updateMailSender(mailSenderSaveDTO, operatorCode)) {
            return Result.success();
        }
        return Result.fail(ResultCodeEnum.SAVE_DATA_FAILED);
    }


}
